//
//  TTImageDingzhiChooseView.m
//  TaoTuo
//
//  Created by 刘广 on 2020/3/27.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTImageDingzhiChooseView.h"

@interface TTImageDingzhiChooseView ()

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UITextField *contentTF;
@property (nonatomic, strong) UIButton *topBtn;

@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) id target;
@property (nonatomic, assign) SEL action;

@end

@implementation TTImageDingzhiChooseView

- (instancetype)initWithFrame:(CGRect)frame title:(nonnull NSString *)title target:(nonnull id)target action:(nonnull SEL)action
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.title = title;
        self.target = target;
        self.action = action;
        
        [self setupSubviews];
        
    }
    return self;
}

- (void)setContent:(NSString *)content
{
    _content = content;
    self.contentTF.text = content;
}

- (void)setupSubviews
{
    self.titleLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:15];
    self.titleLabel.attributedText = [self getAttrStringWithString:self.title];
    [self addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.width.mas_equalTo(100);
        make.top.bottom.equalTo(self);
    }];
    
    self.contentTF = [[UITextField alloc] init];
    self.contentTF.layer.cornerRadius = 5;
    self.contentTF.font = [UIFont systemFontOfSize:15];
    self.contentTF.backgroundColor = UIColorHex(#F0F4F7);
    self.contentTF.enabled = NO;
    [self addSubview:self.contentTF];
    
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, self.height)];
    self.contentTF.leftView = leftView;
    self.contentTF.leftViewMode = UITextFieldViewModeAlways;
    
    [self.contentTF mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.equalTo(self.titleLabel.mas_right);
       make.right.equalTo(self).mas_offset(-10);
       make.top.bottom.equalTo(self);
    }];
    
    self.topBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.topBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [self.topBtn setImage:[UIImage imageNamed:@"cellarrow_3"] forState:UIControlStateNormal];
    [self.topBtn addTarget:self.target action:self.action forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.topBtn];
    [self.topBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_right);
        make.right.equalTo(self).mas_offset(-15);
        make.top.bottom.equalTo(self);
    }];
    
}


- (NSMutableAttributedString *)getAttrStringWithString:(NSString *)string
{
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:string];
    if ([string hasPrefix:@"*"]) {
        [attr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[string rangeOfString:@"*"]];
    }
    
    return attr;
    
}



@end
